package com.lkd.controller;


import cn.hutool.core.bean.BeanUtil;
import com.lkd.dto.RegionDto;
import com.lkd.entity.Region;
import com.lkd.service.RegionService;
import com.lkd.vo.PageBean;
import com.lkd.vo.RegionVo;
import com.lkd.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 运营区域 前端控制器
 * </p>
 *
 * @author 聂德祥
 * @since 2024-08-17
 */
@RestController
public class RegionController {
@Autowired
    private RegionService regionService;
// 分页查询运营区域
    @GetMapping("/api/vm-service/region/search")
    public Result search(String name, @RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "10") Integer pageSize){
        PageBean<RegionVo> page = regionService.search(name,pageIndex,pageSize);
        return Result.success(page);
    }
    // 添加运营区域
    @PostMapping("/api/vm-service/region")
    public Result addRegion(@RequestBody RegionDto regionDto){
        Region region = BeanUtil.copyProperties(regionDto, Region.class);
        region.setName(regionDto.getRegionName());
        regionService.save(region);
        return Result.success();
    }
    // 按id查询运营区域
    @GetMapping("/api/vm-service/region/{id}")
    public Result getRegionById(@PathVariable Integer id){
        Region region = regionService.getById(id);
        return Result.success(region);
    }
    // 修改运营区域
    @PutMapping("/api/vm-service/region")
    public Result updateRegion(Region region){
        regionService.updateById(region);
        return Result.success();
    }
//     删除运营区域
    @DeleteMapping("/api/vm-service/region/{id}")
    public Result deleteRegion(@PathVariable Integer id){
        regionService.removeById(id);
        return Result.success();
    }
}
